package project.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import schronisko.MySQLConnection;

/**
 * Servlet implementation class AnimalAdd
 */
@WebServlet("/AnimalAdd")
public class AnimalAdd extends HttpServlet {
	private static final long serialVersionUID = 1L;
	HttpSession s;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AnimalAdd() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter out = response.getWriter();

		response.setContentType("text/html");
		String name=request.getParameter("name");
		String race=request.getParameter("race");
		String gender=request.getParameter("gender");
		String date=request.getParameter("data");
		String last_inoculation = request.getParameter("last_inoculation");
		String description = request.getParameter("description");
		s = request.getSession();
		s.setAttribute("name_anim", name);
		s.setAttribute("date_anim", date);
		try{
			// Wys�anie zapytania do bazy
			MySQLConnection.connect().executeUpdate("INSERT INTO animal(name, race, gender, date_of_birth, accessibility, add_date, last_inoculation, description) VALUES ('"+name+"','"+race+"','"+gender+"','"+date+"','pending', CURDATE(),'"+ last_inoculation + "','"+ description + "')");
			//zamkni�cie po��czenia z baz�
    		MySQLConnection.Close();
    		// // ustawianie zmiennej w sesji - pierwszy parametr to nazwa zmiennej, drugi parametr to jej wartosc
    		
    		
    		s.setAttribute("added", 1);
    		//out.println("Zwierzaczek zostal dodany.<br> Zaraz zostaniesz przekierowany na strone glowna");
    		// przekierowanie na strone g��wn�
    		response.sendRedirect("worker/new.jsp");
        	//response.setHeader("Refresh", "3; URL=index.jsp");
		}
		catch(Exception e){
			//out.println("Nie udalo sie doda� :(<br> Zaraz zostaniesz przekierowany na strone glowna");
    		// przekierowanie na strone g��wn�
			s.setAttribute("added", 4);
			response.sendRedirect("worker/new.jsp");
        	response.setHeader("Refresh", "3; URL=worker/added.jsp");
		}
	
	}

}
